﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

/// <summary>
/// SystemItems 的摘要说明
/// </summary>
namespace Housing
{
    public class SystemItems
    {
        private int _id;
        private string _avgincome;
        private string _totlepro;
        private string _otherrentstandard;
        private string _lowrentstandard;
        private string _onlyonearea;
        private string _avgarea;
        private string _standardlowarea;
        private string _updatedate;
        private string _username;

        public SystemItems()
        {
        }

        public int Sys_id
        {
            get { return _id; }
            set { _id = value; }
        }

        public string Sys_avgincome
        {
            get { return _avgincome; }
            set { _avgincome = value; }
        }

        public string Sys_totlepro
        {
            get { return _totlepro; }
            set { _totlepro = value; }
        }

        public string Sys_otherrentstandard
        {
            get { return _otherrentstandard; }
            set { _otherrentstandard = value; }
        }

        public string Sys_lowrentstandard
        {
            get { return _lowrentstandard; }
            set { _lowrentstandard = value; }
        }

        public string Sys_onlyonearea
        {
            get { return _onlyonearea; }
            set { _onlyonearea = value; }
        }

        public string Sys_avgarea
        {
            get { return _avgarea; }
            set { _avgarea = value; }
        }

        public string Sys_standardlowarea
        {
            get { return _standardlowarea; }
            set { _standardlowarea = value; }
        }

        public string Sys_updatedate
        {
            get { return _updatedate; }
            set { _updatedate = value; }
        }

        public string Sys_username
        {
            get { return _username; }
            set { _username = value; }
        }

        //方法
        protected string connString = SqlHelper.ConnectionString;

        public bool AddNewSystemItem(SystemItems systemitem)
        {
            string query = @"INSERT INTO SystemItems(sys_avgincome,sys_totlepro,sys_otherrentstandard,sys_lowrentstandard,sys_onlyonearea,sys_avgarea,
                        sys_standardlowarea,sys_updatedate,sys_username) VALUES('" + systemitem.Sys_avgincome + "','" + systemitem.Sys_totlepro + "','" + systemitem.Sys_otherrentstandard + "',";
            query += "'" + systemitem.Sys_lowrentstandard + "','" + systemitem.Sys_onlyonearea + "','" + systemitem.Sys_avgarea + "','" + systemitem.Sys_standardlowarea + "','" + systemitem.Sys_updatedate + "','" + systemitem.Sys_username + "')";
            bool result = Convert.ToBoolean(SqlHelper.ExecuteNonQuery(connString, CommandType.Text, query));
            return result;
        }

        public bool UpdateASystemItem(SystemItems systemitem)
        {
            string query = "UPDATE SystemItems SET sys_avgincome='" + systemitem.Sys_avgincome + "',sys_totlepro='" + systemitem.Sys_totlepro + "',sys_otherrentstandard='" + systemitem.Sys_otherrentstandard + "',";
            query += "sys_lowrentstandard='" + systemitem.Sys_lowrentstandard + "',sys_onlyonearea='" + systemitem.Sys_onlyonearea + "',sys_avgarea='" + systemitem.Sys_avgarea + "',";
            query += "sys_standardlowarea='" + systemitem.Sys_standardlowarea + "',sys_updatedate='" + systemitem.Sys_updatedate + "',sys_username='" + systemitem.Sys_username + "' WHERE sys_id=" + systemitem.Sys_id;
            bool result = Convert.ToBoolean(SqlHelper.ExecuteNonQuery(connString, CommandType.Text, query));
            return result;
        }

        public DataSet GetAllSystemItems()
        {
            string query = "SELECT * FROM SystemItems";
            DataSet ds = SqlHelper.ExecuteDataset(connString, CommandType.Text, query);
            return ds;
        }

        public void GetNewestSystemItem()
        {
            string query = "SELECT TOP 1 * FROM SystemItems ORDER BY sys_updatedate DESC";
            SqlDataReader dr = SqlHelper.ExecuteReader(connString, CommandType.Text, query);
            if (dr.Read())
            {
                Sys_id = (int)dr[0];
                Sys_avgincome = Utils.GetString(dr[1]);
                Sys_totlepro = Utils.GetString(dr[2]);
                Sys_otherrentstandard = Utils.GetString(dr[3]);
                Sys_lowrentstandard = Utils.GetString(dr[4]);
                Sys_onlyonearea = Utils.GetString(dr[5]);
                Sys_avgarea = Utils.GetString(dr[6]);
                Sys_standardlowarea = Utils.GetString(dr[7]);
                Sys_updatedate = Utils.GetString(dr[8]);
                Sys_username = Utils.GetString(dr[9]);
            }
            dr.Close();
        }
        //该历史记录表暂时没有删除功能
        //public bool DeleteASystemItem(int id)
        //{
        //    string query="DELETE FROM SystemItems WHERE sys_id="+id;
        //    bool result = Convert.ToBoolean(SqlHelper.ExecuteNonQuery(connString,CommandType.Text,query));
        //    return result;
        //}
    }
}